﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using LIMS.BLL;
using LIMS.Model;


namespace LIMS.Web
{
    public partial class AdmLabOrder : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                bindLabIDs();
                bindTeacher();
                AspNetPager1.RecordCount = LabOrderManager.GetAllOrder().Count;
                rpOrderLabManager.DataSource = Common.CommonPager<Model.OrderLabs>.GetTSourceByPager(BLL.LabOrderManager.GetAllOrder(), AspNetPager1.PageSize, 1);
                rpOrderLabManager.DataBind();

            }
        }

        private void bindTeacher()
        {
            selTeacher.DataSource = TeacherManager.GetAllTeacher();
            selTeacher.DataTextField = "teacherName";
            selTeacher.DataValueField = "teacherID";
            selTeacher.DataBind();
            selTeacher2.DataSource = TeacherManager.GetAllTeacher();
            selTeacher2.DataTextField = "teacherName";
            selTeacher2.DataValueField = "teacherID";
            selTeacher2.DataBind();
        }
        public String ConventTeaIdToName(String teaID)
        {
            return BLL.TeacherManager.GetNameByID(teaID);
        }
        public String ConventEnumToString(String strClassTimeCode)
        {
            OrderClassTimeSpan classTimeSpan = (OrderClassTimeSpan)Enum.Parse(typeof(OrderClassTimeSpan), strClassTimeCode);
            String result = null;
            switch (classTimeSpan)
            {
                case OrderClassTimeSpan.labStatus12:
                    result = "上午一二节";
                    break;
                case OrderClassTimeSpan.labStatus34:
                    result = "上午三四节";
                    break;
                case OrderClassTimeSpan.labStatus56:
                    result = "下午一二节";
                    break;
                case OrderClassTimeSpan.labStatus78:
                    result = "下午三四节";
                    break;
                case OrderClassTimeSpan.labStatus910:
                    result = "晚上一二节";
                    break;
                default:
                    break;
            }
            return result;
        }

        private void bindLabIDs()
        {
            selLabs.DataSource = BLL.LabManager.GetLabIDs().OrderBy(ob => ob.Length);
            selLabs.DataBind();
            selLabs2.DataSource = BLL.LabManager.GetLabIDs().OrderBy(ob => ob.Length);
            selLabs2.DataBind();
        }

        protected void AspNetPager1_PageChanged(object sender, EventArgs e)
        {
            rpOrderLabManager.DataSource = Common.CommonPager<Model.OrderLabs>.GetTSourceByPager(BLL.LabOrderManager.GetAllOrder(),
                                                                                                    AspNetPager1.PageSize, AspNetPager1.CurrentPageIndex);
            rpOrderLabManager.DataBind();
        }

        /// <summary>
        /// 多种查询方式，获取实验室预约信息
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnSingleContonation_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrWhiteSpace(myHiddenField.Value))
            {
                //默认按照实验日期查询
                Model.OrderLabs orderTmp = new Model.OrderLabs();
                orderTmp.orderTestDate = DateTime.Parse(txtDatePicker.Text.Trim());
                rpOrderLabManager.DataSource = BLL.LabOrderManager.Search(orderTmp);
                rpOrderLabManager.DataBind();
            }
            else
            {
                Model.OrderLabs order = new Model.OrderLabs();
                //分别按照 实验日期、实验室、教师、上课时间段、组合搜索
                QueryOrderData(e, order);
            }
        }

        /// <summary>
        /// 分别按照 实验日期、实验室、教师、上课时间段、组合搜索
        /// </summary>
        /// <param name="e">事件源</param>
        /// <param name="order">预约实体类</param>
        private void QueryOrderData(EventArgs e, Model.OrderLabs order)
        {
            //获取隐藏域的值，判断那个标签被激活，进而调用相应的查询方式
            QueryOrder queryOrder = (QueryOrder)Int32.Parse(myHiddenField.Value);

            #region 实验日期、实验室、教师、上课时间段、组合搜索
            switch (queryOrder)
            {
                case QueryOrder.None:
                    break;
                case QueryOrder.ProjectDate:
                    order.orderTestDate = DateTime.Parse(txtDatePicker.Text.Trim());
                    rpOrderLabManager.DataSource = BLL.LabOrderManager.Search(order);
                    rpOrderLabManager.DataBind();
                    break;
                case QueryOrder.LabID:
                    order.labID = selLabs.Value;
                    rpOrderLabManager.DataSource = BLL.LabOrderManager.Search(order);
                    rpOrderLabManager.DataBind();
                    break;
                case QueryOrder.TeacherID:
                    order.teacherID = selTeacher.Value;
                    rpOrderLabManager.DataSource = BLL.LabOrderManager.Search(order);
                    rpOrderLabManager.DataBind();
                    break;
                case QueryOrder.orderClassTimeSpan:
                    Model.OrderClassTimeSpan orderClass = (OrderClassTimeSpan)Int32.Parse(selLabStatus.Value);
                    order.orderClassTimeSpan = orderClass;
                    rpOrderLabManager.DataSource = BLL.LabOrderManager.Search(order);
                    rpOrderLabManager.DataBind();
                    break;
                case QueryOrder.ZuQuery:
                    btnFullTextSearch_Click(this, e);
                    break;
                default:
                    break;
            }
            #endregion
        }

        /// <summary>
        /// 刷新所有预约数据
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnFlushAdmin_Click(object sender, EventArgs e)
        {
            rpOrderLabManager.DataSource = LabOrderManager.GetAllOrder();
            rpOrderLabManager.DataBind();
        }

        /// <summary>
        /// 简要全文搜索，查询实验室预约信息
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnFullTextSearch_Click(object sender, EventArgs e)
        {
            //获取布置的组合标签 tags   --->预约实验日期、实验室、预约上课时间段、教师ID
            List<String> tagKeys = new List<string> { };
            if (!String.IsNullOrWhiteSpace(txtDatePicker2.Text.Trim()))
            {
                tagKeys.Add(txtDatePicker2.Text.Trim());
            }
            tagKeys.Add(selLabs2.Value);
            tagKeys.Add(selLabStatus2.Value);
            tagKeys.Add(selTeacher2.Value);


            //调用后台系统方法，并从新绑定数据
            var orderlist = BLL.LabOrderManager.GetAllOrder();
            System.Data.DataTable dtOrder = Common.ConventerCommon.LinqToDataTable<Model.OrderLabs>(orderlist);

            //获取标签组合
            String[] keywords = new string[] { selTeacher2.Value, selLabs2.Value, selTeacher2.Value };

            //全文搜索的核心调用
            System.Data.DataView rowOrder = Common.ConventerCommon.FullTextSearch(dtOrder, keywords);

            if (rowOrder.Count < 1)
            {
                this.Page.ClientScript.RegisterStartupScript(this.GetType(), "", "alert('没有相关预约数据!)';", true);
                return;
            }
            else
            {
                rpOrderLabManager.DataSource = rowOrder;
                rpOrderLabManager.DataBind();
            }

        }




    }
}